// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import DataManager from "../manager/DataManager";
import PoolManager from "../manager/PoolManager";
import ResourceManager from "../manager/ResourceManager";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";
import { privacyChannel } from "../Script/privacyChannel";
import { prv } from "../Script/prv";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SkinLayer extends BaseLayer {

    panel: cc.Node = null
    btnClose: cc.Node = null
    btnGet: cc.Node = null
    scrollContent: cc.Node = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnClose = cc.find('btn_close', this.panel)
        this.btnGet = cc.find('btn_get', this.panel)
        this.scrollContent = cc.find('scroll/view/content', this.panel)
        this.btnClose.on('click', this.onCloseClick, this)
        this.btnGet.on('click', this.onGetClick, this)
    }

    onDestroy() {
        this.btnClose.off('click', this.onCloseClick, this)
        this.btnGet.off('click', this.onGetClick, this)
    }
    show(): void {
        super.show()
        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    let style = {
                        top: prv.getSysInfo().windowHeight / 2 + 270
                    };
                    prv.showInter(false, style);
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "vivo":
            case "vivo1":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    let style = {
                        top: prv.getSysInfo().windowHeight / 2 + 270
                    };
                    prv.showInter(false, style);
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p2")) {


                    prv.showInter(false);
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "ks":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                }
                break
        }
    }
    onEnable() {
        this.zoomIn(this.panel)
        this.rendor()
        this.rendorPowerTimer()
        SdkManager.instance.toggleCustomRowAd(true)
    }

    onDisable() {
        SdkManager.instance.toggleCustomRowAd(false)
    }

    rendor() {
        if (this.scrollContent.children.length) return
        for (let i = 0; i < DataManager.instance.skinTotal; i++) {
            const skinIndex = i + 1
            const skinItem = PoolManager.instance.getNode('SkinItem', this.scrollContent)
            const skinOn = skinItem.getChildByName('on')
            const skinOff = skinItem.getChildByName('off')
            const skinIcon = skinOn.getChildByName('icon')
            const skinTick = skinOn.getChildByName('tick')
            skinIcon.getComponent(cc.Sprite).spriteFrame = ResourceManager.instance.getSprite(`bottle${skinIndex}`)
            skinTick.active = skinIndex == DataManager.instance.skinIndex
            skinOn.active = DataManager.instance.hasSkinIndex(skinIndex)
            skinOff.active = !DataManager.instance.hasSkinIndex(skinIndex)
            if (!skinOn.hasEventListener('click')) skinOn.on('click', () => { this.onSkinClick(skinIndex) }, this)
        }
        this.btnGet.active = DataManager.instance.skinTotal > DataManager.instance.skinUnlocked.length
    }

    reset() {
        for (let i = 0; i < this.scrollContent.children.length; i++) {
            const skinIndex = i + 1
            const skinItem = this.scrollContent.children[i]
            const skinOn = skinItem.getChildByName('on')
            const skinOff = skinItem.getChildByName('off')
            const skinIcon = skinOn.getChildByName('icon')
            const skinTick = skinOn.getChildByName('tick')
            skinIcon.getComponent(cc.Sprite).spriteFrame = ResourceManager.instance.getSprite(`bottle${skinIndex}`)
            skinTick.active = skinIndex == DataManager.instance.skinIndex
            skinOn.active = DataManager.instance.hasSkinIndex(skinIndex)
            skinOff.active = !DataManager.instance.hasSkinIndex(skinIndex)
            if (!skinOn.hasEventListener('click')) skinOn.on('click', () => { this.onSkinClick(skinIndex) }, this)
        }
        this.btnGet.active = DataManager.instance.skinTotal > DataManager.instance.skinUnlocked.length
    }

    onSkinClick(index: number) {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        DataManager.instance.skinIndex = index
        DataManager.instance.save()
        this.reset()
    }

    onCloseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SKIN, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
    }

    onGetClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        privacyChannel.getChannel().showVideo(() => {

            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SKIN_GET)
        })
    }
}
